$('#watch-more').hide();

$(function () {
    $('.weui-tab').tab({
        defaultIndex: 1,
        activeClass: 'weui-bar__item_on',
        onToggle: function (index) {
            if (index === 0) {
                location.href = "/page/product";
            } else if (index === 1) {
            } else if (index === 2) {
                location.href = "/page/my";
            }
        }
    });
    getWatchProduct();
});

function getWatchProduct() {
    $.ajax({
        type: "GET",
        url: "/product/get-user-products",
        crossDomain: true,
        beforeSend: function () {
            $("#watch-more").show();
        },
        success: function (result) {
            document.getElementById("watch-more").style.display = "none";
            if (result.code === "0") {
                $("#watch-rank-list").append(tpl(document.getElementById('watch-tpl').innerHTML, result.data));
                $('.weui-cell_swiped').swipeout();

                $('.delete-swipeout').click(function () {
                    var id = $(this).parents('.weui-cell').data('id');
                    var jsonData = {"skuid": id, "watchStatus": 1};
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        contentType: "application/json",
                        url: "/product/watch-or-unwatch-product",
                        data: JSON.stringify(jsonData),
                        success: function (result) {
                            if (result.code === "0") {

                            } else if (result.code === "101001") {
                                // location.href = "/page/login";
                                $.toast.prototype.defaults.duration = 1000;//1秒
                                $.toast(result.message, 'text');
                            } else {
                                $.toast.prototype.defaults.duration = 1000;//1秒
                                $.toast(result.message, 'text');
                            }
                        },
                        error: function () {
                            $.toast.prototype.defaults.duration = 1000;//1秒
                            $.toast("无网络", 'text');
                        }
                    });
                    $(this).parents('.weui-cell').remove();
                });

                $('.close-swipeout').click(function () {
                    $(this).parents('.weui-cell').swipeout('close')
                });
                document.getElementById("watch-none").style.display = "block";
                document.getElementById("watch-more").style.display = "none";
            } else if (result.code === "101001") {
                // window.location.href = "/page/login";
                $.toast.prototype.defaults.duration = 1000;//1秒
                $.toast(result.message, 'text');
            } else {
                $.toast.prototype.defaults.duration = 1000;//1秒
                $.toast(result.message, 'text');
            }
        },
        error: function () {
            document.getElementById("watch-more").style.display = "none";
            $.toast.prototype.defaults.duration = 1000;//1秒
            $.toast("无网络", 'text');
        },
        timeout: 15000
    });
}

$(document).on('tap click', "#watch-rank-list .weui-cell .weui-cell", function () {
    var id = $(this).parents('.weui-cell').data('id');
    location.href = "/page/detail?skuid=" + id;
});